<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ $t('order.newAdd') /*新增*/ }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column show-overflow-tooltip type="selection"></el-table-column> -->
      <el-table-column
        show-overflow-tooltip
        prop="name"
        :label="$t('order.nameOfGame') /*玩法名称*/"
      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        :label="$t('common.operate') /*操作*/"
        width="300"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row, 'changci')">
            {{ $t('order.setSession') /*设置场次*/ }}
          </el-button>
          <el-button type="text" @click="handleEdit(row, 'rules')">
            {{ $t('order.setRules') /*设置规则*/ }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('accountManage.delete') /*删除*/ }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.limit"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <changci-management-edit
      ref="changci"
      @fetch-data="fetchData"
    ></changci-management-edit>
    <rules-management-edit
      ref="rules"
      @fetch-data="fetchData"
    ></rules-management-edit>
  </div>
</template>

<script>
import { getScenes, delScene } from '@/api/business.js'
import { doDelete, getList } from '@/api/userManagement'
import userData from '@/views/accountManagement/userManagement/data.json'
import ChangciManagementEdit from './components/changciManagementEdit'
import RulesManagementEdit from './components/rulesManagementEdit'
import Edit from './components/UserManagementEdit'
export default {
  name: 'UserManagement',
  components: { Edit, ChangciManagementEdit, RulesManagementEdit },
  data() {
    return {
      list: null,
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      selectRows: '',
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        limit: 10,
        username: '',
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    setSelectRows(val) {
      this.selectRows = val
    },
    handleEdit(row, type = 'edit') {
      if (row.sc_id) {
        this.$refs[type].showEdit(row)
      } else {
        this.$refs[type].showEdit()
      }
    },
    handleDelete(row) {
      if (row.sc_id) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await delScene({ sc_id: row.sc_id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join()
          this.$baseConfirm('你确定要删除选中项吗', null, async () => {
            const { msg } = await doDelete({ ids })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      }
    },
    handleSizeChange(val) {
      this.queryForm.limit = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    queryData() {
      this.queryForm.page = 1
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const { data } = await getScenes(this.queryForm)

      this.list = data.data
      this.total = data.total
      setTimeout(() => {
        this.listLoading = false
      }, 300)
    },
  },
}
</script>
